package com.abel.manager.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.abel.domain.Application;
import com.abel.domain.Person;
import com.abel.domain.PersonSearchCriteria;
import com.abel.domain.WorkExperience;
import com.abel.manager.WorkExperienceManager;
import com.abel.manager.exception.WorkExperienceException;
import com.abel.repository.WorkExperienceRepository;
import com.abel.repository.exception.RepositoryException;
import com.abel.util.UserSessionLocator;

/**
 * @author BG
 *
 */
public class WorkExperienceManagerImpl implements WorkExperienceManager {
		
	    protected final Log logger = LogFactory.getLog(getClass());
	    
	    private WorkExperienceRepository workExperienceRepository;

		@Override
		public void updateWorkExperience(WorkExperience we)
				throws WorkExperienceException {
			
			Application application = UserSessionLocator.getUserSession().getPerson().getApplication();
			
	        if (we.getWrkHistId() == 0) {
	            try {
	                
	            	int wrkHistId = this.workExperienceRepository.addWorkExperience(we);
	            	we.setWrkHistId(wrkHistId);
	                
	                //add work experience as a new list if no work experience exists in the list
					List<WorkExperience> list = null;
					
					if (application.getWorkExperiences() == null
							|| application.getWorkExperiences().size() == 0) {
						
						list = new ArrayList<WorkExperience>();
					}
					else {
						
						list = application.getWorkExperiences();
					}
					list.add(we);
					application.setWorkExperiences(list);
	                
	            }
	            catch (RepositoryException e)
	            {
	                logger.debug("@WorkExperienceManagerImpl : exception " + e);
	                e.printStackTrace();
	                throw new WorkExperienceException("Exception caused while adding WorkExperience information for " + we.getAplId(), e);
	            }
	     
	        }
	        else {
	            try {
	            	
	                this.workExperienceRepository.updateWorkExperience(we);
	                
	                //update existing work experience list assuming that the list exists
	                List<WorkExperience> list = application.getWorkExperiences();
	                if (list == null
	                		|| list.size() == 0) {
	                	throw new WorkExperienceException("Updating non existant work experience for user id " + UserSessionLocator.getUserSession().getPerson().getUserId());
	                }
	                //setting the updated education
	                for (WorkExperience workExperienceItem : list) {
	                	if (we.getWrkHistId() == workExperienceItem.getWrkHistId()) {
	                		workExperienceItem = we;
	                		break;
	                	}
	                }

	            
	            }
	            catch (RepositoryException e) {
	            	
	                logger.debug("@WorkExperienceRepositoryImpl : exception " + e);
	                e.printStackTrace();
	                throw new WorkExperienceException("Exception caused while updating WorkExperience information for " + we.getAplId(), e);
	            }
	     
	        }

		}

		
		public void deleteWorkExperience(int aplId, int wrkHistId) throws WorkExperienceException
		{
			    try {

			    	this.workExperienceRepository.deleteWorkExperience(aplId, wrkHistId);

			    	//getting work experience list
			        List<WorkExperience> list = UserSessionLocator.getUserSession().getPerson().getApplication().getWorkExperiences();
			        
			        //deleting the specific work experience from threadlocal list
			        for (WorkExperience workExperienceItem : list) {
	                	if (wrkHistId == workExperienceItem.getWrkHistId()) {
	                		list.remove(workExperienceItem);
	                		break;
	                	}
	                }
			    }
			    catch (RepositoryException e)
			    {
			        logger.debug("@WorkExperienceRepositoryImpl : exception " + e);
			        e.printStackTrace();
			        throw new WorkExperienceException("Exception caused while deleting WorkExperience information for " + wrkHistId, e);
			    }
		}

		
		public void deleteWorkExperience(int wrkHistId) throws WorkExperienceException
		{
			    try {
			        this.workExperienceRepository.deleteWorkExperience(wrkHistId);
				    
			        //getting work experience list
			        List<WorkExperience> list = UserSessionLocator.getUserSession().getPerson().getApplication().getWorkExperiences();
			        
			        //deleting the specific work experience from threadlocal list
			        for (WorkExperience workExperienceItem : list) {
	                	if (wrkHistId == workExperienceItem.getWrkHistId()) {
	                		list.remove(workExperienceItem);
	                		break;
	                	}
	                }

			    }
			    catch (RepositoryException e)
			    {
			        logger.debug("@WorkExperienceRepositoryImpl : exception " + e);
			        e.printStackTrace();
			        throw new WorkExperienceException("Exception caused while deleting WorkExperience information for " + wrkHistId, e);
			    }
		}

		
		/* (non-Javadoc)
		 * @see com.abel.manager.WorkExperienceManager#getWorkExperienceInformation(com.abel.domain.PersonSearchCriteria)
		 */
		@Override
		public List<WorkExperience> getWorkExperience(PersonSearchCriteria criteria)
				throws WorkExperienceException {
			
			
			Person person = UserSessionLocator.getUserSession().getPerson();
			try {
				List<WorkExperience> list = workExperienceRepository.getWorkExperience(criteria);
				
				
				if (list.size() > 0) {
					
					//setting the gross pay mode
					for (WorkExperience workExperience : list) {
						
						if (workExperience.getWrkGrossAnnualPay() > 0) {
							workExperience.setWrkPayMode("annual");
							workExperience.setWrkGrossPay(workExperience.getWrkGrossAnnualPay());
						}
						else {
							workExperience.setWrkPayMode("hourly");
							workExperience.setWrkGrossPay(workExperience.getWrkGrossHourlyPay());
						}
						
					}
					person.getApplication().setWorkExperiences(list);
				}
				return list;
			} 
			catch (RepositoryException e) {
				
				e.printStackTrace();
				throw new WorkExperienceException("Exception caused while deleting WorkExperience information for " + criteria.getUserName(), e);
			}
		}

		@Override
		public void setWorkExperienceRepository(WorkExperienceRepository workExperienceRepository) 
		{
			this.workExperienceRepository = workExperienceRepository;
		}

}
